package com.pioneer.swing;


import com.pioneer.swing.ui.MainMenu;
import com.pioneer.swing.ui.ManifestsFrame;
import com.pioneer.swing.ui.ManifestEntryFrame;
import com.pioneer.swing.ui.ContainerEntryFrame;
import com.pioneer.swing.ui.UIConstants;
import com.pioneer.swing.controller.WindowOpener;
import com.pioneer.swing.controller.WindowRefresher;
import com.pioneer.dao.*;
import com.pioneer.util.AppProperties;

import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.UIManager;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class PioneerApp {		
	
	//private static final String GTK = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" ;
	private static final String WINDOWS = "windows";
	private static final String UNIX = "unix";
	private static final String JAVA = "java";
	private static final String METAL_CLASS = "javax.swing.plaf.metal.MetalLookAndFeel" ;
	private static final String WINDOWS_CLASS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ;
	private static final String MOTIF_CLASS = "com.sun.java.swing.plaf.motif.MotifLookAndFeel" ;
	private static String currentRole = null;
	//private ManifestDAO manifestDao;
	

	
	public PioneerApp()
		throws Exception {
		// load view
		
		JFrame mainFrame = new JFrame(UIConstants.TITLE_MAIN_MENU);
		JDesktopPane desktopPane = new JDesktopPane();
		mainFrame.setContentPane(desktopPane);
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int inset = 50;
		mainFrame.setBounds(inset,inset,screenSize.width-inset*2,screenSize.height-inset*2);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		
			
		// load model
		
		try {
			//Properties dbprops=new Properties();
			//dbprops.load(new FileInputStream(FileConstant.CONFIG_PATH+java.io.File.separator+FileConstant.DBPROP));
			//Properties ldapprops=new Properties();
			//ldapprops.load(new FileInputStream(FileConstant.CONFIG_PATH+java.io.File.separator+FileConstant.LDAPPROP));
			ManifestDAO manifestDAO=new JdbcManifestDAO(AppProperties.getProperties());			
			ContactDAO jobQuoteContactDAO = new LdapContactDAO(AppProperties.getProperties(),
				AppProperties.getProperty(AppProperties.JOB_QUOTES_LDAP_CONTEXT));
			ContactDAO metalsContactDAO = new LdapContactDAO(AppProperties.getProperties(),
				AppProperties.getProperty(AppProperties.METALS_LDAP_CONTEXT));
			JobDAO jobDAO = new JdbcJobDAO(AppProperties.getProperties());
			SecurityDAO securityDAO = new JdbcSecurityDAO(AppProperties.getProperties());
			//Properties pathProps = new Properties();
			//pathProps.load(new FileInputStream(FileConstant.CONFIG_PATH+File.separator+FileConstant.PATHPROP));				
			//String userDir = AppProperties.getProperty(AppProperties.USERDIR);
			//String userfileLocation = userDir+File.separator+AppProperties.getProperty(AppProperties.USERFILE);				
			//Properties userprops = new Properties();
			//userprops.load(new java.io.FileInputStream(userfileLocation));
			String username = AppProperties.getProperty(AppProperties.USERNAME);
			String password = AppProperties.getProperty(AppProperties.PASSWORD);
			if (!securityDAO.isAuthenticated(username,password)) {
				System.err.println("Sorry but the username/password in the file is invalid");
				System.exit(1);
			}
			currentRole = securityDAO.getRole(username);			
			setSkin(AppProperties.getProperty(AppProperties.SKIN));
			// load controllers
		
			WindowOpener menuListener=new WindowOpener(desktopPane);
			menuListener.setJobQuoteContactDAO(jobQuoteContactDAO);
			menuListener.setMetalsContactDAO(metalsContactDAO);
			menuListener.setManifestDAO(manifestDAO);
			manifestDAO.getManifests();
			menuListener.setJobDAO(jobDAO);
			mainFrame.setJMenuBar(new MainMenu(menuListener));
		}
		catch (java.io.IOException ex) {
			throw new DataAccessException("Couldn't load properties for DAO because "+ex.getMessage());
		}		
		mainFrame.setVisible(true);
	}
	
	public static String getCurrentRole() {
			return currentRole;
		}
	
	private void setSkin(String skin)
	throws Exception{
		if (skin.equals(UNIX)) {
			UIManager.setLookAndFeel(MOTIF_CLASS);
		} else if (skin.equals(WINDOWS)) {
			UIManager.setLookAndFeel(WINDOWS_CLASS);
		} else if (skin.equals(JAVA)){
			UIManager.setLookAndFeel(METAL_CLASS);
		}
	}

	public static void main(String[] args) {
		try {
			AppProperties.init();
			if (args.length==0) {				
				//Properties pathProps = new Properties();
				//pathProps.load(new FileInputStream(FileConstant.CONFIG_PATH+java.io.File.separator+FileConstant.PATHPROP));
				String userDir = AppProperties.getProperty(AppProperties.USERDIR);
				String stdErr = AppProperties.getProperty(AppProperties.ERRORFILE);
				String stdOut = AppProperties.getProperty(AppProperties.LOGFILE);
				System.setErr(new PrintStream(new FileOutputStream(userDir+File.separator+stdErr)));
				System.setOut(new PrintStream(new FileOutputStream(userDir+File.separator+stdOut)));			
				System.getProperties().list(System.out);
			}
			
			PioneerApp pioneerMetals = new PioneerApp();
			//
			
		}
		catch (Exception ex) {
			ex.printStackTrace(System.err);
		}
	}
}
